package Student;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class S_addstu extends JPanel implements ActionListener{
    JTextField 学号,姓名;
    JButton 录入;
    public S_addstu(){
        try
        {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
        catch(Exception e){System.err.println("不能设置外观: "+e);}
        学号=new JTextField(20);
        姓名=new JTextField(20);

        录入=new JButton("录入");
        录入.addActionListener(this);
        Box box1=Box.createHorizontalBox();//横放 box
        Box box2=Box.createHorizontalBox();

        Box box3=Box.createHorizontalBox();
        box1.add(new JLabel("学  号:"/*,JLabel.CENTER*/));
        box1.add(学号);
        box2.add(new JLabel("姓  名:"/*,JLabel.CENTER*/));
        box2.add(姓名);
        box3.add(录入);
        Box boxH=Box.createVerticalBox();//竖放 box
        boxH.add(box1);
        boxH.add(box2);
        boxH.add(box3);
        boxH.add(Box.createVerticalGlue());
        JPanel messPanel=new JPanel();
        messPanel.add(boxH);
        setLayout(new BorderLayout());
        add(messPanel,BorderLayout.CENTER);
        validate();
    }
    public void actionPerformed(ActionEvent c){
        Object obj=c.getSource();
        if(obj==录入){
            if( 学号 .getText().equals("")|| 姓名 .getText().equals("")){
                JOptionPane.showMessageDialog(this,"学生信息请填满 再录入！ " );
            }
            Statement stmt=null;
            ResultSet rs1=null;
            String sql,sql1;
            sql1="select * from xuesheng where xuesheng_id='"+学号.getText()+"'";
            sql="insert into xuesheng values('"+ 学号 .getText()+"','"+ 姓名.getText()+"')";
            try{
                Connection dbConn1=CONN();
                stmt=(Statement)dbConn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                rs1=stmt.executeQuery(sql1);
                if(rs1.next()){JOptionPane.showMessageDialog(this," 该 学号以存在， 无法添加");}
else{
                        stmt.executeUpdate(sql);
                        JOptionPane.showMessageDialog(this,"添加成功");
                    }
                    rs1.close();
                    stmt.close();
                }
catch(SQLException e){
                    System.out.print("SQL Exception occur.Message is:"+e.getMessage());
                }
            }
        }
//连接数据库方法
        public static Connection CONN(){
            String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加 载JDBC 驱动
            String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=master"; //连接服务器和数据库 test
            String userName = "sa"; //默认用户名
            String userPwd = "123456"; //密码
            Connection dbConn=null;
            try {
                Class.forName(driverName);
                dbConn = DriverManager.getConnection(dbURL, userName,
                        userPwd);
                System.out.println("Connection Successful!"); //如果连接成功 控制台输出 Connection Successful!
            } catch (Exception e) {
                e.printStackTrace();
            }
            return dbConn;
        }
    }